package com.xneure.database.define;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.xneure.utils.ObjectUtil;

public class TableDefineImpl extends TableDefine{
	private String name;
	private String comment;
	private String constraint;
	Map<String,ColumnDefine> columns;

	public String getName(){
		return name;
	}

	public void setName(String name){
		this.name = name;
	}

	String getConstraint(){
		return constraint;
	}

	void setConstraint(String constraint){
		this.constraint = constraint;
	}

	protected Map<String,ColumnDefine> getColumnsByMap(){
		return columns;
	}
	public ColumnDefine[] getColumns(){
		return columns.values().toArray(new ColumnDefine[columns.size()]);
	}

	public void addColumn(ColumnDefine col){
		if(ObjectUtil.isNull(columns)){
			columns = new LinkedHashMap<>();
		}
		if(columns.containsKey(col.getName().toUpperCase())){
			throw new RuntimeException("列重复：".concat(col.getName()));
		}
		columns.put(col.getName().toUpperCase(),col);
	}

	@Override
	public ColumnDefine getColumn(String name){
		return columns.get(name.toUpperCase());
	}

	@Override
	public ColumnDefine getColumn(int index){
		int point = 0;
		for(Entry<String,ColumnDefine> ent:columns.entrySet()){
			if(++point == index){
				return ent.getValue();
			}
		}
		return null;
	}

	public String getComment(){
		return comment;
	}

	public void setComment(String comment){
		this.comment = comment;
	}
	
}
